package day13.edu;
// Created:13/09/2025, ChangJiang Ru
public class WindowTest3 {
    public static void main(String[] args) {
        Window2 w = new Window2();

        Thread w1 = new Thread(w);
        Thread w2 = new Thread(w);
        Thread w3 = new Thread(w);

        w1.setName("Thread1");
        w2.setName("Thread2");
        w3.setName("Thread3");

        w1.start();
        w2.start();
        w3.start();
    }
}

class Window2 implements Runnable
{
    static int ticket = 100;
    Object obj = new Object();

    @Override
    public void run()
    {
        while(true)
        {
            try {
                Thread.sleep(5);
            }catch(InterruptedException e)
            {
                e.printStackTrace();
            }
            synchronized(obj)
            {
                if(ticket > 0)
                {
                    System.out.println(Thread.currentThread().getName() + " have sale, the number is : " + ticket);
                    ticket--;
                }else
                    break;
            }
        }
    }
}
